package controller.wechat;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import util.api.ApIURL;
import util.httpclient.HttpService;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by thinkjoy on 2017/9/6.
 * 生成二维码
 */
@Controller
public class WechatQrCode {

    @Autowired
    private HttpService httpService;

    /**
     * 生成二维码并返回二维码链接
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    public String getQrCode() throws IOException, URISyntaxException {
        Map<String,Object> value=new HashMap<>();
        Map<String,Object> item=new HashMap<>();
        Map<String,Object> item1=new HashMap<>();
        value.put("expire_seconds",2592000);
        value.put("action_name","QR_STR_SCENE");
        item1.put("scene_str","thinkjoy");
        item.put("scene",item1);
        value.put("action_info",item);
        String qr=httpService.doPostAsJson(ApIURL.QR_CODE_TEMP_URL+ApIURL.ACCESS_TOKEN, JSON.toJSONString(value));
        String ticket=JSON.parseObject(qr).getString("ticket");
        ticket=URLEncoder.encode(ticket,"utf-8");
        String qrurl=ApIURL.QR_EXCHANGE_URL+ticket;
        return qrurl;
    }
}
